#!/bin/sh
#
# Maintenance script
# 
set -e

usage () {
    echo "Maintenance script for Gauche."
    echo "Usage: DIST command"
    echo "Commands:"
    echo "  gen    Generate configure scripts"
    echo "  tgz    Generate distribution tarball"
    echo "  clean-build-test   Run make maintainer-clean, then fully build and"
    echo "                     run tests.  Requires newest release of gosh in"
    echo "                     PATH."
    echo "  self-host-test     In a separate directly, first build the current"
    echo "                     source tree with installed gosh, then build the"
    echo "                     source tree again using the freshly compiled"
    echo "                     gosh, and run tests."
    exit 0
}

maintainer_clean () {
  if [ -f Makefile ]; then make maintainer-clean; fi
  for xdir in spigot mqueue-cpp; do
    if [ -f examples/$xdir/Makefile ]; then
      (cd examples/$xdir; make maintainer-clean)
    fi
  done

  find . -path '*/.git' -prune -o -type f -name '*~' -exec rm -- {} +

  rm -f DIST_EXCLUDE_X
  cat DIST_EXCLUDE > DIST_EXCLUDE_X
}

check_version () {
  if [ ! -f VERSION ]; then echo "No VERSION; something wrong?"; exit 1; fi
  VERSION=`cat VERSION`
}

do_gen () {
    maintainer_clean
    rm -f configure gc/configure gc/configure.gnu
    cp tools/gc-configure.gnu gc/configure.gnu
    if [ "$LIBTOOLIZE" = "" -a "`uname`" = "Darwin" ]; then
        if command -v "glibtoolize" > /dev/null; then
            LIBTOOLIZE=glibtoolize
        elif command -v "libtoolize" > /dev/null; then
            LIBTOOLIZE=libtoolize
        else
            echo "DIST: line $LINENO: command libtoolize or glibtoolize not found"
            exit 1
        fi
    fi
    ${LIBTOOLIZE:-libtoolize} -q --copy  # ensures ltmain.sh in the top_srcdir
    autoconf
    (cd gc; ./autogen.sh)
}

# Creates a release tarabll as ../Gauche-$VERSION.tgz
do_tgz () {
    tools/make-tgz.sh
}

do_pdf () {
    ./DIST gen
    ./configure --enable-threads=pthreads --enable-multibyte=utf8
    make
    (cd doc; make gauche-refe.pdf; mv gauche-refe.pdf ../../Gauche-$VERSION-refe.pdf)
}

# hidden command - only meaningful on the release engineer's machine
do_release () {
    do_tgz
    do_pdf
    gpg --detach-sign --armor -o ../Gauche-$VERSION.tgz.asc ../Gauche-$VERSION.tgz
}

do_mingw_installer () {
    do_gen
    MSYSTEM=MINGW64 src/mingw-dist.sh --with-gl --with-installer
    MSYSTEM=MINGW32 src/mingw-dist.sh --with-gl --with-installer
}

do_clean_build_test () {
    do_gen
    gauche-config --reconfigure | sh
    make -j
    make -s check
}

do_self_host_test () {
    do_gen
    srcdir=`pwd`
    destdir=`pwd`/../Gauche-tmp-self-host-test
    # mingw doesn't seem to like parallel make
    if [ x$MSYSTEM = x ]; then
	makeopt=-j
    else
	makeopt=
    fi
    makeopt_1="$makeopt BUILD_GOSH_FLAGS=$BUILD_GOSH_FLAGS"
    if [ -d $destdir/stage1 ]; then rm -rf $destdir; fi
    XPATH=$destdir/bin:$PATH
    mkdir -p $destdir/stage1
    (cd $destdir/stage1; $srcdir/configure --prefix=$destdir; make $makeopt_1; make install)
    mkdir -p $destdir/stage2
    (cd $destdir/stage2; PATH=$XPATH $srcdir/configure; PATH=$XPATH make $makeopt; PATH=$XPATH make -s check)
}

case $1 in
    gen)   do_gen ;;
    tgz)   do_tgz ;;
    release) do_release ;;
    mingw-installer)  do_mingw_installer ;;
    clean-build-test) do_clean_build_test ;;
    self-host-test)   do_self_host_test ;;
    *) usage ;;
esac
